<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="utf-8" lang="utf-8">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>
   数组辅助函数 - CodeIgniter 中文手册|用户手册|用户指南|Wiki文档
  </title>
  <link rel="shortcut icon" href="/user_guide/../images/design/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet" type="text/css" media="all" href="/user_guide/userguide.css" />
  <link rel="search" href="/user_guide/../CodeIgniterSearch.xml" type="application/opensearchdescription+xml" title="CodeIgniter 搜索" />
  <link rel="canonical" href="/user_guide/helpers/array_helper.html" />
  <script type="text/javascript" src="/user_guide/nav/nav.js">
  </script>
  <script type="text/javascript" src="/user_guide/nav/prototype.lite.js">
  </script>
  <script type="text/javascript" src="/user_guide/nav/moo.fx.js">
  </script>
  <script type="text/javascript" src="/user_guide/nav/user_guide_menu.js">
  </script>
  <meta name="robots" content="all" />
  <meta name="author" content="ExpressionEngine Dev Team" />
  <meta name="description" content="CodeIgniter 中文手册, CodeIgniter 用户指南, CodeIgniter User Guide, Wiki 文档" />
 </head>
 <body>
  <!-- START NAVIGATION -->
  <div id="nav">
   <div id="nav_inner">
    <script type="text/javascript">
     create_menu('/user_guide/');
    </script>
   </div>
  </div>
  <script type="text/javascript">
   _setNavigation();
  </script>
  <div id="nav2">
   <a name="top">
   </a>
   <a href="javascript:void(0);" onclick="myHeight.toggle();">
    <img src="/user_guide/images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="切换目录" alt="切换目录" />
   </a>
  </div>
  <div id="masthead">
   <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
    <tr>
     <td width="350">
      <h1>
       CodeIgniter 用户指南 版本 2.0.0
      </h1>
     </td>
     
     <td id="breadcrumb_right">
      <a href="/user_guide/toc.html">
       目录页
      </a>
     </td>
    </tr>
   </table>
  </div>
  <!-- END NAVIGATION -->
  <!-- START BREADCRUMB -->
  <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
   <tr>
    <td id="breadcrumb">
     <a href="/" target="_blank">
      CodeIgniter 中国首页
     </a>
     &nbsp;&#8250;&nbsp;
     <a href="/user_guide/toc.html">用户指南目录</a>
     &nbsp;&#8250;&nbsp;数组辅助函数
    </td>
    <td id="searchbox">
     <form method="get" action="http://www.google.com/search" target="google_window">
      <input type="hidden" name="client" value="pub-0176846097796333" />
      <input type="hidden" name="forid" value="1" />
      <input type="hidden" name="ie" value="UTF-8" />
      <input type="hidden" name="oe" value="UTF-8" />
      <input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.org.cn/user_guide/" />
      搜索用户指南&nbsp;
      <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />
      &nbsp;
      <input type="submit" class="submit" name="sa" value="Go" />
     </form>
    </td>
   </tr>
  </table>
  <!-- END BREADCRUMB -->
  <div style="clear:both;text-align:right;padding: 6px 40px 0 0;">
   <a href="http://codeigniter.com/user_guide/helpers/array_helper.html" target="_blank">
    查看原文
   </a>
  </div>
  <!--<br clear="all"  />-->
  <!-- START CONTENT -->
  <div id="content">
   <h1>
    数组辅助函数
   </h1>
   <p>
    数组辅助函数的文件涵盖了一些用于辅助数组操作的函数。
   </p>
   <h2>
    装载本辅助函数
   </h2>
   <p>
    本辅助函数的装载通过如下代码完成：
   </p>
   <p>
    <code>
     $this-&gt;load-&gt;helper('array');
    </code>
   </p>
   <p>
    可用的函数如下：
   </p>
   <h2>
    element()
   </h2>
   <p>
    获取数组中的元素。本函数测试数组的索引是否已设定并含有数值。如果已设有数值则返回该数值，否则返回 FALSE，或任何你设定的默认数值（函数第三个参数）。范例：
   </p>
   <p>
    <code>
     $array = array('color' =&gt; 'red', 'shape' =&gt; 'round', 'size' =&gt; '');
     <br />
     <br />
     // 返回 &quot;red&quot;
     <br />
     echo element('color', $array);
     <br />
     <br />
     // 返回 NULL
     <br />
     echo element('size', $array, NULL);
    </code>
   </p>
   <h2>
    random_element()
   </h2>
   <p>
    根据提供的数组，随机返回该数组内的一个元素。使用范例：
   </p>
   <p>
    <code>
     $quotes = array(
     <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;I find that the harder I work, the more luck I seem to have. - Thomas Jefferson&quot;,
     <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Don't stay in bed, unless you can make money in bed. - George Burns&quot;,
     <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;We didn't lose the game; we just ran out of time. - Vince Lombardi&quot;,
     <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;If everything seems under control, you're not going fast enough. - Mario Andretti&quot;,
     <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Reality is merely an illusion, albeit a very persistent one. - Albert Einstein&quot;,
     <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Chance favors the prepared mind - Louis Pasteur&quot;
     <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);
     <br />
     <br />
     echo random_element($quotes);
    </code>
   </p>
   <h2>
    elements()
   </h2>
   <p>
    Lets you fetch a number of items from an array.  The function tests whether each of the array indices is set.  If an index does not exist
it is set to FALSE, or whatever you've specified as the default value via the third parameter.  Example:
   </p>
   <p>
    <strong>
     试译：
    </strong>
    该函数从一个数组中取得若干元素。该函数测试（传入）数组的每个键值是否在（目标）数组中已定义；如果一个键值不存在，该键值所对应的值将被置为FALSE，或者你可以通过传入的第3个参数来指定默认的值。例如：
   </p>
   <code>
    $array = array(
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'color' => 'red',
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'shape' => 'round',
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'radius' => '10',
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'diameter' => '20'
    <br />
    );
    <br />
    <br />
    $my_shape = elements(array('color', 'shape', 'height'), $array);
    <br />
   </code>
   <p>
    The above will return the following array:
   </p>
   <p>
    <strong>
     试译：
    </strong>
    上面的程序将返回下面的数组：
   </p>
   <code>
    array(
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'color' => 'red',
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'shape' => 'round',
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'height' => FALSE
    <br />
    );
   </code>
   <p>
    You can set the third parameter to any default value you like:
   </p>
   <p>
    <strong>
     试译：
    </strong>
    你可以将第3个参数设为任何你想要的默认值：
   </p>
   <code>
    $my_shape = elements(array('color', 'shape', 'height'), $array, NULL);
    <br />
   </code>
   <p>
    The above will return the following array:
   </p>
   <p>
    <strong>
     试译：
    </strong>
    上面的程序将返回下面的数组：
   </p>
   <code>
    array(
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'color' => 'red',
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'shape' => 'round',
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;'height' => NULL
    <br />
    );
   </code>
   <p>
    This is useful when sending the
    <kbd>
     $_POST
    </kbd>
    array to one of your Models.  This prevents users from
sending additional POST data to be entered into your tables:
   </p>
   <p>
    <strong>
     试译：
    </strong>
    这（种方法）在将
    <kbd>
     $_POST
    </kbd>
    数组传入你的模型时非常有用。通过这种方式可以防止用户发送的额外的 POST 数据进入你的数据表：
   </p>
   <code>
    $this-&gt;load-&gt;model('post_model');
    <br />
    <br />
    $this-&gt;post_model-&gt;update(elements(array('id', 'title', 'content'), $_POST));
   </code>
   <p>
    This ensures that only the id, title and content fields are sent to be updated.
   </p>
   <p>
    <strong>
     试译：
    </strong>
    这样保证了只有 id, title 和 content 字段被发送以进行更新。
   </p>
   <p>
    &nbsp;
   </p>
   <div id="Contributors">
    翻译贡献者:
canglan, Hex, zhaosusen
   </div>
   <div id="DocDate">
    最后修改: 2011-03-23 14:32:56
   </div>
  </div>
  <!-- END CONTENT -->
  <div id="footer">
   <p>
    上一个主题:&nbsp;&nbsp;
    <a href="/user_guide/libraries/zip.html">
     Zip 编码类
    </a>
    &nbsp;&nbsp;&middot;&nbsp;&nbsp;
    <a href="#top">
     页首
    </a>
    &nbsp;&middot;&nbsp;&nbsp;
    <a href="/user_guide/toc.html">用户指南目录</a>
    &nbsp;&nbsp;&middot;&nbsp;&nbsp;下一个主题:&nbsp;&nbsp;
    <a href="/user_guide/helpers/compatibility_helper.html">
     兼容性辅助函数
    </a>
   </p>
   <p>
    <a href="http://codeigniter.com">
     CodeIgniter
    </a>
    &nbsp;&middot;&nbsp; 版权所有 &#169; 2006-2011 &nbsp;&middot;&nbsp;
    <a href="http://ellislab.com/">
     Ellislab, Inc.
    </a>
   </p>
   <p>
    中文化:
    <a href="">
     CodeIgniter 中国
    </a>
    &nbsp;&middot;&nbsp; 制作: Hex &nbsp;&middot;&nbsp; 版本: 1.20 &nbsp;&middot;&nbsp; 鸣谢: 子非鱼
   </p>
  </div>
  
  <div style="display:none">
   
   <noscript>
    <a href="http://www.51.la/?3289908" target="_blank">
     <img alt="我要啦免费统计" src="http://img.users.51.la/3289908.asp" style="border:none" />
    </a>
   </noscript>
  </div>
 </body>
</html>